package com.yunpuvip.sachet.biz.service.impl;

import com.yunpuvip.sachet.biz.entity.Player;
import com.yunpuvip.sachet.biz.mapper.PlayerMapper;
import com.yunpuvip.sachet.biz.model.params.PlayerParam;
import com.yunpuvip.sachet.biz.model.result.PlayerResult;
import  com.yunpuvip.sachet.biz.service.PlayerService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunpuvip.sachet.modular.page.LayuiPageFactory;
import com.yunpuvip.sachet.modular.page.LayuiPageInfo;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 会员用户 服务实现类
 * </p>
 *
 * @author wushaohong
 * @since 2019-08-10
 */
@Service
public class PlayerServiceImpl extends ServiceImpl<PlayerMapper, Player> implements PlayerService {

    @Override
    public void add(PlayerParam param){
        Player entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(PlayerParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(PlayerParam param){
        Player oldEntity = getOldEntity(param);
        Player newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PlayerResult findBySpec(PlayerParam param){
        return null;
    }

    @Override
    public List<PlayerResult> findListBySpec(PlayerParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(PlayerParam param){
        Page pageContext = getPageContext();
        QueryWrapper<Player> objectQueryWrapper = new QueryWrapper<>();
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(PlayerParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Player getOldEntity(PlayerParam param) {
        return this.getById(getKey(param));
    }

    private Player getEntity(PlayerParam param) {
        Player entity = new Player();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public Player getPlayer(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Player playerWxLogin(String token, Integer id) {
        return null;
    }
}
